package com.itboye.mengwu.app.adapter;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

import android.R.integer;
import android.content.Context;
import android.content.Intent;
import android.support.v4.app.FragmentActivity;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.itboye.config.GlobalConfig;
import com.itboye.core.object.SpPublicParams;
import com.itboye.core.utils.SPUtils;
import com.itboye.core.utils.XImageLoader;
import com.itboye.inageview.ImagePagerActivity;
import com.itboye.inageview.NoScrollGridAdapter;
import com.itboye.inageview.NoScrollGridView;
import com.itboye.mengwu.R;
import com.itboye.mengwu.app.ui.mymeng.IntegerParse;
import com.itboye.mengwu.data.model.AllData;
import com.itboye.mengwu.data.model.LuntanTieZiBean;
import com.itboye.mengwu.data.model.ShopCarXiangQing;
import com.itboye.mengwu.data.model.GoodsCommentsModel.GoodsComments;
import com.itboye.mengwu.data.model.ShopCarNext;
import com.itboye.mengwu.data.model.TieZiBaoHanReply.GetAList;

/** GirdView 数据适配器 */
public class MengTimesAdapter extends BaseAdapter {

	private Context mContext;
	private LuntanTieZiBean items;
	ArrayList<GetAList> aLists;
	OnClickListener editOnclickListener;
	String t_month;
	String t_year;
	String t_day;
	String t_week;
	String t_noon;
	String uid;

	public MengTimesAdapter(Context ctx, LuntanTieZiBean items) {
		this.mContext = ctx;
		this.items = items;
	}

	public MengTimesAdapter(Context context, ArrayList<GetAList> aLists,
			OnClickListener editOnclickListener) {
		// TODO Auto-generated constructor stub
		this.aLists = aLists;
		mContext = context;
		this.editOnclickListener = editOnclickListener;
	}

	@Override
	public int getCount() {
		return aLists == null ? 0 : aLists.size();
	}

	@Override
	public Object getItem(int position) {
		return aLists.get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		ViewHolder holder;
		if (convertView == null) {
			holder = new ViewHolder();
			convertView = View.inflate(mContext, R.layout.item_mengshiguang,
					null);
			holder.content = (TextView) convertView.findViewById(R.id.content);
			holder.txt_time1 = (TextView) convertView
					.findViewById(R.id.txt_time1);
			holder.txt_time2 = (TextView) convertView
					.findViewById(R.id.txt_time2);
			holder.txt_date = (TextView) convertView
					.findViewById(R.id.txt_date);
			holder.txt_month = (TextView) convertView
					.findViewById(R.id.txt_month);
			holder.img_1 = (ImageView) convertView.findViewById(R.id.img_1);
			holder.img_2 = (ImageView) convertView.findViewById(R.id.img_2);
			holder.img_3 = (ImageView) convertView.findViewById(R.id.img_3);
			holder.img_4 = (ImageView) convertView.findViewById(R.id.img_4);
			holder.img_5 = (ImageView) convertView.findViewById(R.id.img_5);
			holder.img_type_2 = (LinearLayout) convertView
					.findViewById(R.id.img_type_2);
			holder.img_type_3 = (LinearLayout) convertView
					.findViewById(R.id.img_type_3);
			holder.img_type_1 = (ImageView) convertView
					.findViewById(R.id.img_type_1);
			holder.edit = (ImageView) convertView.findViewById(R.id.edit);
			convertView.setTag(holder);
		} else {
			holder = (ViewHolder) convertView.getTag();
		}
		holder.txt_date.setText(aLists.get(position).getDatelineShow());
		uid = SPUtils.get(mContext, null, SpPublicParams.UID, "") + "";
		Log.e("hu...", "uid=" + uid + "autid"
				+ aLists.get(position).getAuthorId());
		if (uid.equals(aLists.get(position).getAuthorId())) {
			holder.edit.setVisibility(View.VISIBLE);
		} else {
			holder.edit.setVisibility(View.GONE);
		}
		holder.edit.setTag(position);
		holder.edit.setOnClickListener(editOnclickListener);
		holder.content.setText(aLists.get(position).getMessage());
		holder.img_type_1.setImageBitmap(null);
		holder.img_2.setImageBitmap(null);
		holder.img_3.setImageBitmap(null);
		holder.img_4.setImageBitmap(null);
		holder.img_5.setImageBitmap(null);
		holder.img_1.setImageBitmap(null);
		String time = aLists.get(position).getDateline();
		DateChange(time);
		holder.txt_month.setText(t_day);
		holder.txt_date.setText(t_year + "." + t_month);
		holder.txt_time1.setText(t_week);
		holder.txt_time2.setText(t_noon);
		// 获取指定日期转换成星期几
		if (aLists.get(position).getImg() != null) {
			if (aLists.get(position).getImg().size() == 0) {
				holder.img_type_2.setVisibility(View.GONE);
				holder.img_type_3.setVisibility(View.GONE);
				holder.img_type_1.setVisibility(View.GONE);
			}
			if (aLists.get(position).getImg().size() == 1) {
				XImageLoader.load(GlobalConfig.API_URL + "/picture/index?id="
						+ aLists.get(position).getImg().get(0),
						holder.img_type_1);
				holder.img_type_2.setVisibility(View.GONE);
				holder.img_type_3.setVisibility(View.GONE);
				holder.img_type_1.setVisibility(View.VISIBLE);
			} else if (aLists.get(position).getImg().size() == 2) {
				XImageLoader.load(GlobalConfig.API_URL + "/picture/index?id="
						+ aLists.get(position).getImg().get(0), holder.img_1);
				XImageLoader.load(GlobalConfig.API_URL + "/picture/index?id="
						+ aLists.get(position).getImg().get(1), holder.img_2);
				holder.img_type_1.setVisibility(View.GONE);
				holder.img_type_3.setVisibility(View.GONE);
				holder.img_type_2.setVisibility(View.VISIBLE);
			} else if (aLists.get(position).getImg().size() == 3) {
				XImageLoader.load(GlobalConfig.API_URL + "/picture/index?id="
						+ aLists.get(position).getImg().get(0), holder.img_3);
				XImageLoader.load(GlobalConfig.API_URL + "/picture/index?id="
						+ aLists.get(position).getImg().get(1), holder.img_4);
				XImageLoader.load(GlobalConfig.API_URL + "/picture/index?id="
						+ aLists.get(position).getImg().get(2), holder.img_5);
				holder.img_type_2.setVisibility(View.GONE);
				holder.img_type_1.setVisibility(View.GONE);
				holder.img_type_3.setVisibility(View.VISIBLE);
			}

		} else {
			holder.img_type_2.setVisibility(View.GONE);
			holder.img_type_3.setVisibility(View.GONE);
			holder.img_type_1.setVisibility(View.GONE);
		}
		return convertView;
	}

	/**
	 * 打开图片查看器
	 * 
	 * @param position
	 * @param urls2
	 */
	protected void imageBrower(int position, ArrayList<String> urls2) {
		Intent intent = new Intent(mContext, ImagePagerActivity.class);
		// 图片url,为了演示这里使用常量，一般从数据库中或网络中获取
		intent.putExtra(ImagePagerActivity.EXTRA_IMAGE_URLS, urls2);
		intent.putExtra(ImagePagerActivity.EXTRA_IMAGE_INDEX, position);
		mContext.startActivity(intent);
	}

	/**
	 * listview组件复用，防止“卡顿”
	 * 
	 * @author Administrator
	 * 
	 */
	class ViewHolder {
		// private ImageView iv_avatar;
		// private TextView tv_title;
		// private TextView tv_content;
		// private NoScrollGridView gridview;
		//
		// private TextView imgZan;
		TextView content, txt_time1, txt_time2, txt_date, txt_month;
		ImageView img_1, img_2, img_3, img_4, img_5, img_type_1;
		LinearLayout img_type_2, img_type_3;
		ImageView edit;

	}

	private void DateChange(String date) {
		Calendar calendar2 = Calendar.getInstance();
		long timews = Long.valueOf(date);
		Date dates = new Date(timews * 1000L);
		calendar2.setTime(dates);
		int year = calendar2.get(Calendar.YEAR);
		int month = calendar2.get(Calendar.MONTH);
		int day_year = calendar2.get(Calendar.DAY_OF_MONTH);
		t_day = day_year + "";
		int day = calendar2.get(Calendar.DAY_OF_WEEK);
		t_year = year + "";
		t_month = month + 1 + "";
		if (day == 1) {
			t_week = "星期日";
		} else if (day == 2) {
			t_week = "星期一";
		} else if (day == 3) {
			t_week = "星期二";
		} else if (day == 4) {
			t_week = "星期三";
		} else if (day == 5) {
			t_week = "星期四";
		} else if (day == 6) {
			t_week = "星期五";
		} else if (day == 7) {
			t_week = "星期六";
		}
		SimpleDateFormat myFmt2 = new SimpleDateFormat("HH");
		String afternoon = myFmt2.format(Long.parseLong(date) * 1000L);
		if (Integer.parseInt(afternoon) < 12) {
			t_noon = "上午";
		} else {
			t_noon = "下午";
		}

	}
}